﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class FriendLinkService : IFriendLinkService
    {
        private IFriendLinkManager _ifr;
        public FriendLinkService(IFriendLinkManager ifr)
        {
            _ifr = ifr;
        }

        public async Task<int> AddUsersAsync(string title, string link)
        {
            return await _ifr.AddAsync(new Model.FriendLink 
            {
                Title = title,
                Link = link
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _ifr.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ifr.DeleteAsync(data);
        }

        public async Task<List<FriendLinkDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _ifr.GetChooseAsync(fr => fr.DeleteId == deleteId);
            return data == null ? new List<FriendLinkDto>() : data.Select(fr => new FriendLinkDto 
            {
                Id = fr.Id,
                Title = fr.Title,
                Link = fr.Link,
                CreateTime = fr.CreateTime,
                UpdateTime = fr.UpdateTime
            }).ToList();
        }

        public async Task<FriendLinkDto> GetDateById(int id)
        {
            var data = await _ifr.GetById(id);
            return data == null ? new FriendLinkDto() : new FriendLinkDto 
            {
                Id = data.Id,
                Title = data.Title,
                Link = data.Link,
                UpdateTime = data.UpdateTime,
                CreateTime = data.CreateTime
            };
        }

        public async Task<List<FriendLinkDto>> GetDateByName(string title, int deleteId = 1)
        {
            var data = await _ifr.GetChooseAsync(fr => fr.Title.Contains(title) && fr.DeleteId == deleteId);
            return data == null ? new List<FriendLinkDto>() : data.Select(fr => new FriendLinkDto 
            {
                Id = fr.Id,
                Title = fr.Title,
                Link = fr.Link,
                CreateTime = fr.CreateTime,
                UpdateTime = fr.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ifr.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ifr.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ifr.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ifr.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersAsync(int id, string title, string link)
        {
            var data = await _ifr.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Link = link;
            data.UpdateTime = DateTime.Now;
            return await _ifr.UpdateAsync(data);
        }
    }
}
